
package cn.com.xy.sms.sdk.util;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import cn.com.xy.sms.sdk.constant.Constant;
import cn.com.xy.sms.sdk.log.LogManager;
import cn.com.xy.sms.sdk.smsmessage.BusinessSmsMessage;


public class PopupMsgManager {

	public static LinkedList<BusinessSmsMessage> businessSmsList = new LinkedList<BusinessSmsMessage>();
	  public static synchronized int addAllToFirst(LinkedList<BusinessSmsMessage> list){
		 int res =-1;
		  int size=list.size()-1;
		  if(size >= 0){
			  
			  BusinessSmsMessage temp=null;
			  int size2= businessSmsList.size();
			  for(int i =size ;i >= 0;i--){
				  temp = list.get(i);
				  Object v=temp.getValue("opensms_enable");
			 
				  String addr = StringUtils.getPhoneNumberNo86(temp.getOriginatingAddress());//且不包含在号码移除列表中
		    		if(!(v != null && "false".equalsIgnoreCase(v.toString())) && !removePhoneNumList.contains(addr)){
				        businessSmsList.addFirst(temp);
		    		}else{
		    			if(size2 ==0 && size ==0){
		    			  businessSmsList.addFirst(temp);
		    			}
		    			res=1;  
		    		}
			  }
		  }
		  removePhoneNumList.clear();
		  list.clear();
		  return res;
	  }

    public static synchronized BusinessSmsMessage getBussinessMessageByIndex(int index) {
        try {
            if (index >= 0 && index < businessSmsList.size()) {
                return businessSmsList.get(index);
            }
        } catch (Throwable e) {
//            e.printStackTrace();
            LogManager.e(Constant.TAG, "getBussinessMessageByIndex: ", e);

        }
        return null;
    }

    public static synchronized BusinessSmsMessage removeBusinessMessageByIndex(int index) {
        try {
            return businessSmsList.remove(index);
        } catch (Throwable e) {
//            e.printStackTrace();
            LogManager.e(Constant.TAG, "removeBusinessMessageByIndex: ", e);

        }
        return null;
    }
    public static synchronized boolean removeBusinessMessage(BusinessSmsMessage msg) {
        try {
           return   businessSmsList.remove(msg);
        } catch (Throwable e) {
//            e.printStackTrace();
            LogManager.e(Constant.TAG, "removeBusinessMessage: ", e);

        }
        return false;
    }

   
    public static synchronized void clearBusinessMessage(List<BusinessSmsMessage> list) {
    	int size =0;
    	if(list != null){
    	       businessSmsList.removeAll(list);
    	       size = list.size();
    	}
    }

    public static synchronized int getBusinessMessageSize() {
        return businessSmsList.size();
    }
    /**
     * 清除用户手动点击弹窗的
     * @return
     */
    public static synchronized int clearUserClickBusinessMessage() {
    	int size=businessSmsList.size();
    	List<BusinessSmsMessage> list = new ArrayList<BusinessSmsMessage>();
    	for(int i= 0;i <size;i++){
    		Object v=businessSmsList.get(i).getValue("opensms_enable");
    		if(v != null && "false".equalsIgnoreCase(v.toString())){
    			list.add(businessSmsList.get(i));
    		}
    	}
    	/*SDK-646 zhaojiangwei 20161122 start */
    	if(!list.isEmpty()){
    	/*SDK-646 zhaojiangwei 20161122 end */
    	 businessSmsList.removeAll(list);
    	 list.clear();
    	 return 1;
    	}
    	list=null;
    	return -1;
    }
    public static boolean hasRemoveData=false;//是否有移除数据
    public static boolean getHasRemoveData(){
        return hasRemoveData;
    }
    public static void setHasRemoveData(boolean value){
        hasRemoveData = value;
    }
    public static List<String> removePhoneNumList=new ArrayList<String>();
    public static void removeBusinessMessageByNum(Context ctx,String phoneNumber,boolean normalRmove,Map<String,String> extend){
    	try {
    		if(StringUtils.isNull(phoneNumber))return;
			if(businessSmsList!=null&&!businessSmsList.isEmpty())
			{
				Iterator<BusinessSmsMessage> itemItor =businessSmsList.iterator();					
				while(itemItor != null && itemItor.hasNext()){
					BusinessSmsMessage item= itemItor.next();					
					if(item!=null)
					{
						String no86 = StringUtils.getPhoneNumberNo86(phoneNumber);
						String no86OriginatingAddress = StringUtils.getPhoneNumberNo86(item.getOriginatingAddress());
						if(!StringUtils.isNull(no86)&&!StringUtils.isNull(no86OriginatingAddress)
								&&no86.equals(no86OriginatingAddress))
						{
							
							if(normalRmove ==false){//不是正常移除,进入会话详情,需要清除历史缓存
								if(!removePhoneNumList.contains(no86)){
									  removePhoneNumList.add(no86);
									}
								itemItor.remove();
								hasRemoveData =true;
							}else if(normalRmove && item.messageBody ==null){//正常移除
								itemItor.remove();
							}
						}
					
					}
				}
			}
		} catch (Throwable e) {
//			e.printStackTrace();
            LogManager.e(Constant.TAG, "removeBusinessMessageByNum: ", e);

		}
    }
    /**
     * 已有第三方弹窗号码
     */
    public static HashSet<String> hasPhoneThird = new HashSet<String>();

    public static void clearPhoneThird() {
        hasPhoneThird.clear();
    }
    
    public static boolean containsPhoneThird(String phoneNumber) {
        return hasPhoneThird.contains(phoneNumber);
    }
    
    public static void removePhoneThird(String phoneNumber){
        PopupMsgManager.hasPhoneThird.remove(phoneNumber);
    }
    
    public static void clearPopup(){
    	PopupMsgManager.businessSmsList.clear();
    	DuoquUtils.getSdkDoAction().clearPopup();
    }
    
    /**
    *添加第三方弹窗短信
    */
    public static boolean addThirdPopupMsgData(Map<String,Object> msgData){
    	boolean hasItem=false;
		BusinessSmsMessage item=null;
		String phoneNumber=(String)msgData.get("phoneNumber");
		phoneNumber = StringUtils.getPhoneNumberNo86(phoneNumber);
    	if(businessSmsList!=null&&!businessSmsList.isEmpty())
		{
//    		valueMap.put("msgId", msgId);
//			valueMap.put("simIndex", simIndex);
//			valueMap.put("simName", simName);
//			valueMap.put("msgTime", msgTime);
//			valueMap.put("phoneNumber", phoneNumber);
//			valueMap.put("smsContent", smsContent);
    		
			Iterator<BusinessSmsMessage> itemItor =businessSmsList.iterator();	
			

			while(itemItor != null && itemItor.hasNext()){
				  item= itemItor.next();					
				if(item!=null && item.messageBody == null){//说明第三方弹窗数据,判断号码是否一致
					String no86OriginatingAddress = StringUtils.getPhoneNumberNo86(item.getOriginatingAddress());
					if(!StringUtils.isNull(phoneNumber)&&!StringUtils.isNull(no86OriginatingAddress)
							&&phoneNumber.equals(no86OriginatingAddress)){
						 hasItem =true;
						 break;
					}
				
				}
			}
			
		}
    	if(hasItem && item != null){
			//说明已有项
			item.valueMap=msgData;
			item.originatingAddress=phoneNumber;
			businessSmsList.remove(item);
			//businessSmsList.add(item);
			if(!hasPhoneThird.contains(phoneNumber)){
				hasPhoneThird.add(phoneNumber);
			}
		}else{
			//说明没有项
			item = new BusinessSmsMessage();
			item.valueMap=msgData;
			item.originatingAddress=phoneNumber;
		}
    	if(item != null){
		 businessSmsList.addLast(item);
    	}
    	return true;
    }
    	
  /*  public static HashMap<String,View> thirdView = new HashMap<String,View>();
    public static Map<String, ArrayList<HashMap<String, Object>>> thirdMsgData = new HashMap<String,ArrayList<HashMap<String, Object>>>();
    
    public synchronized static void addView(Activity context,String phoneNum,Map<String, Object> msgData,SdkCallBack callback){
    	XyView view = (XyView)thirdView.get(phoneNum);
    	if(view==null)
    	{
    		view = new XyView(context);    		
    	}    	
    	thirdView.put(phoneNum, view);
    	ArrayList<HashMap<String, Object>> myMsgData = thirdMsgData.get(phoneNum);
    	if(myMsgData==null)
    	{
    		myMsgData = new ArrayList<HashMap<String, Object>>();
    	}
    	myMsgData.add((HashMap<String, Object>) msgData);
    	thirdMsgData.put(phoneNum, myMsgData);
    	
    }
*/
}
